package org.practice.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class InitParamFilter implements Filter {

	/*web应用停止或重新部署的时候才销毁，*/
	@Override
	public void destroy() {
	   System.out.println("============销毁InitParamFilter===========");
	  
	}
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request=(HttpServletRequest)servletRequest;
		HttpServletResponse response=(HttpServletResponse)servletResponse;
		request.getSession().getServletContext().log("开始执行过滤器！");
		String command=request.getSession(true).getServletContext().getInitParameter("ControlCommand");
		request.getSession().getServletContext().log("====="+command);
		request.getSession().getServletContext().log("开始执行过滤器！");
		if (command.contains("redirect")) {		
			HttpSession session = request.getSession(true);
			response.sendRedirect("");
		} else {
			chain.doFilter(servletRequest, servletResponse);
		}

	}
	
	/*web应用启动的时候初始化，只初始化一次，以后就可以处理相关请求*/
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("==========初始化InitParamFilter========");
	}

}
